package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.domain.DataResult;
import com.dongdongshop.em.DataResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbContentCategoryService;
import com.dongdongshop.service.TbContentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("TbContentController")
public class TbContentController {

    @Reference
    private TbContentService tbContentService;
    @Reference
    private TbContentCategoryService categoryService;

    @RequestMapping("TbContent")
    public String TbContent(){
        return "admin/content";
    }

    @ResponseBody
    @RequestMapping("selectBys")
    public DataResult selectBys(){
        List<TbContent> selectss = tbContentService.selectss();
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(selectss);
    }

    @RequestMapping("toadd")
    @ResponseBody
    public DataResult toadd(TbContent tbContent){
        boolean insert = tbContentService.insert(tbContent);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(insert);
    }

    @RequestMapping("deleteContent")
    @ResponseBody
    public DataResult deleteContent(@RequestParam("ids[]") Long[] ids){
        tbContentService.deleteByids(ids);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS);
    }

    @RequestMapping("selectByid")
    @ResponseBody
    public DataResult selectByid(Long id){
        try {
            TbContent tbContent = tbContentService.selectByid(id);
            List<TbContentCategory> selectss = categoryService.selectss();
            Map<String,Object> map = new HashMap<>();
            map.put("content",tbContent);
            map.put("list",selectss);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(map);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @ResponseBody
    @RequestMapping("updateContent")
    public DataResult updateContent(TbContent tbContent){
        try {
            Boolean updata = tbContentService.updata(tbContent);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }
}
